package com.zhangzhan.handler;

import com.alibaba.fastjson.JSON;
import com.zhangzhan.domain.commcon.ResponseResult;
import com.zhangzhan.domain.enums.AppHttpCodeEnum;
import com.zhangzhan.utils.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 张一恒
 * @version 1.0
 */
//授权异常捕获
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        //打印异常
        e.printStackTrace();
        //设置状态码和异常信息然后传到浏览器
        ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);

        WebUtils.renderString(httpServletResponse, JSON.toJSONString(result));
    }
}
